package stark.app.function.net_ping_ip;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import stark.app.api.IpQueryApi;
import stark.app.api.NetTestUrlApi;
import stark.common.basic.lifecycle.BaseViewModel;
import stark.common.basic.utils.AESUtil;
import stark.common.basic.utils.LogUtil;

/**
 * create by fanlc
 * on 2020/10/22
 * description
 */
public class NetPingIpViewModel extends BaseViewModel {

    public void requestIpData(String url) {

        Disposable disposable = IpQueryApi.getInstance().ipQuery(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<IpQueryBean>() {
                    @Override
                    public void accept(IpQueryBean bean) throws Exception {
                        LogUtil.e("requestData "+bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        LogUtil.e("requestData Error"+throwable.toString()+"\n");

                    }
                });
        addDisposable(disposable);
    }


    public void requestNetUrlData(NetUrlCallback callback) {
        Disposable disposable = NetTestUrlApi.getInstance().getUrl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<NetUrlBean>() {
                    @Override
                    public void accept(NetUrlBean bean) throws Exception {
                        LogUtil.e("requestData "+bean.getData().getB());
                        LogUtil.e(AESUtil.decrypt(bean.getData().getB()));
                        callback.result(AESUtil.decrypt(bean.getData().getB()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        LogUtil.e("requestData Error"+throwable.toString()+"\n");

                    }
                });
        addDisposable(disposable);
    }

}
